/* eslint-disable no-extra-parens */
import { InitModalMethod, ParseModalMethod } from './defination';

/**
 * 设置表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const initModalData = (form = {}, workstep = {}, tts = false) => {
	if (!form || !workstep || !workstep.do) {
		return;
	}

	form.aiSpeak = workstep.do?.data; // 1
	form.aiSpeakVideo = workstep.do?.video;

	form.confidence = workstep.after.data?.confidence; // 2

	// yes
	if (workstep.answer[0]?.after) {
		form.wordsYes = workstep.answer[0]?.do?.data; // 3
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
		form.nextStepYes = workstep.answer[0]?.after?.id;
	} else {
		form.nextStepYes = workstep.answer[0]?.do?.id;
		form.wordsYesVideo = workstep.answer[0]?.do?.video;
	}
	// no
	if (workstep.answer[1]?.after) {
		form.wordsNo = workstep.answer[1]?.do?.data; // 4
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
		form.nextStepNo = workstep.answer[1]?.after?.id;
	} else {
		form.nextStepNo = workstep.answer[1]?.do?.id;
		form.wordsNoVideo = workstep.answer[1]?.do?.video;
	}
	// other
	if (workstep.answer[2]?.after) {
		form.wordsOther = workstep.answer[2]?.do?.data; // 5
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	} else if (tts) {
		form.wordsOtherVideo = workstep.answer[2]?.do?.video;
	}
	if (workstep.errorRemind?.after) {
		form.wordsEnd = workstep.errorRemind?.do?.data; // 6
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
		form.nextStepOther = workstep.errorRemind?.after?.id;
	} else {
		form.nextStepOther = workstep.errorRemind?.do?.id;
		form.wordsEndVideo = workstep.errorRemind?.do?.video;
	}
	form.otherTimesMax = workstep.errorRemind?.maximum; // 5

	/** 变量存储处理开始 */
	form['aiSpeakVariableId'] = workstep.do.variableId;
	form['aiSpeakVariables'] = workstep.do.variables?.filter(item => form['aiSpeak']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsYesVariableId'] = workstep.answer[0].do.variableId;
	form['wordsYesVariables'] = workstep.answer[0].do.variables?.filter(item => form['wordsYes']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsNoVariableId'] = workstep.answer[1].do.variableId;
	form['wordsNoVariables'] = workstep.answer[1].do.variables?.filter(item => form['wordsNo']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsOtherVariableId'] = workstep.answer[2].do.variableId;
	form['wordsOtherVariables'] = workstep.answer[2].do.variables?.filter(item => form['wordsOther']?.indexOf(`{{${item.name}}}`) !== -1);
	form['wordsEndVariableId'] = workstep.errorRemind.do.variableId;
	form['wordsEndVariables'] = workstep.errorRemind.do.variables?.filter(item => form['wordsEnd']?.indexOf(`{{${item.name}}}`) !== -1);
	/** 变量存储处理结束 */

	form.name = workstep.name;
};
InitModalMethod['idCardCompare'] = initModalData;

/**
 * 解析表单属性
 * @param { Object } form 表单数据
 * @param { Object } workstep cached的ai工作流
 */
export const parseModalData = (form = {}, workstep = {}, tts = false) => {
	console.log(form, workstep);
	if (!form || !workstep || !workstep.do) {
		return;
	}

	workstep.do.data = form.aiSpeak; // 1
	workstep.do.video = form.aiSpeakVideo;

	// confidence
	workstep.after.data.confidence = form.confidence; // 2

	// yes
	if ((form.wordsYes && !tts) || (form.wordsYesVideo && !tts) || (tts && form.wordsYes && form.wordsYesVideo)) {
		workstep.answer[0].do.data = form.wordsYes; // 3
		workstep.answer[0].do.video = form.wordsYesVideo;
		workstep.answer[0].after.id = form.nextStepYes;
		workstep.answer[0].after.no = form.nextStepYes;
	} else {
		workstep.answer[0].do = Object.assign({}, workstep.answer[0].after);
		workstep.answer[0].do.id = form.nextStepYes;
		workstep.answer[0].do.no = form.nextStepYes;
		workstep.answer[0].do.video = form.wordsYesVideo;
		delete workstep.answer[0].after;
	}

	// no
	if ((form.wordsNo && !tts) || (form.wordsNoVideo && !tts) || (tts && form.wordsNo && form.wordsNoVideo)) {
		workstep.answer[1].do.data = form.wordsNo; // 4
		workstep.answer[1].do.video = form.wordsNoVideo;
		workstep.answer[1].after.id = form.nextStepNo;
		workstep.answer[1].after.no = form.nextStepNo;
	} else {
		workstep.answer[1].do = Object.assign({}, workstep.answer[1].after);
		workstep.answer[1].do.id = form.nextStepNo;
		workstep.answer[1].do.no = form.nextStepNo;
		workstep.answer[1].do.video = form.wordsNoVideo;
		delete workstep.answer[1].after;
	}

	// other
	if ((form.wordsOther && !tts) || (form.wordsOtherVideo && !tts) || (tts && form.wordsOther && form.wordsOtherVideo)) {
		workstep.answer[2].do.data = form.wordsOther; // 5
		workstep.answer[2].do.video = form.wordsOtherVideo;
	} else {
		workstep.answer[2].do = Object.assign({}, workstep.answer[2].after);
		workstep.answer[2].do.video = form.wordsOtherVideo;
		delete workstep.answer[2].after;
	}
	if ((form.wordsEnd && !tts) || (form.wordsEndVideo && !tts) || (tts && form.wordsEnd && form.wordsEndVideo)) {
		workstep.errorRemind.after.id = form.nextStepOther;
		workstep.errorRemind.after.no = form.nextStepOther;
		workstep.errorRemind.do.data = form.wordsEnd; // 7
		workstep.errorRemind.do.video = form.wordsEndVideo;
	} else {
		workstep.errorRemind.do = Object.assign({}, workstep.errorRemind.after);
		workstep.errorRemind.do.id = form.nextStepOther;
		workstep.errorRemind.do.no = form.nextStepOther;
		workstep.errorRemind.do.video = form.wordsEndVideo;
		delete workstep.errorRemind.after;
	}
	workstep.errorRemind.maximum = form.otherTimesMax; // 4

	/** 变量存储处理开始 */
	workstep.do.variableId = form['aiSpeakVariableId'];
	workstep.do.variables = form['aiSpeakVariables']?.filter(item => form['aiSpeak'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[0].do.variableId = form['wordsYesVariableId'];
	workstep.answer[0].do.variables = form['wordsYesVariables']?.filter(item => form['wordsYes'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[1].do.variableId = form['wordsNoVariableId'];
	workstep.answer[1].do.variables = form['wordsNoVariables']?.filter(item => form['wordsNo'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.answer[2].do.variableId = form['wordsOtherVariableId'];
	workstep.answer[2].do.variables = form['wordsOtherVariables']?.filter(item => form['wordsOther'].indexOf(`{{${item.name}}}`) !== -1);

	workstep.errorRemind.do.variableId = form['wordsEndVariableId'];
	workstep.errorRemind.do.variables = form['wordsEndVariables']?.filter(item => form['wordsEnd'].indexOf(`{{${item.name}}}`) !== -1);

	/** 变量存储处理结束 */

	workstep.name = form.name;
};
ParseModalMethod['idCardCompare'] = parseModalData;
// {
//     "robotProperty": {
//         "needHearing": false,
//         "videoType": "real-time-fitting",
//         "audioType": "video-native"
//     },
//     "name": "",
//     "do": {
//         "data": "1",
//         "command": "speak",
//         "variables": [],
//         "variableId": ""
//     },
//     "no": "idCardCompare_51",
//     "id": "idCardCompare_51",
//     "type": "idCardCompare",
//     "after": {
//         "data": {
//             "confidence": 2
//         },
//         "command": "toMatchIdCardNoAndPic"
//     },
//     "answer": [
//         {
//             "do": {
//                 "data": "3",
//                 "command": "speak",
//                 "variables": [],
//                 "variableId": ""
//             },
//             "key": "yes",
//             "after": {
//                 "command": "jump-to",
//                 "no": "BlankNode_52",
//                 "id": "BlankNode_52"
//             },
//             "type": "yes"
//         },
//         {
//             "do": {
//                 "data": "4",
//                 "command": "speak",
//                 "variables": [],
//                 "variableId": ""
//             },
//             "key": "no",
//             "after": {
//                 "data": {
//                     "confidence": ""
//                 },
//                 "command": "toMatchIdCardNoAndPic"
//             },
//             "type": "no"
//         },
//         {
//             "do": {
//                 "data": "检测不到人脸",
//                 "command": "speak",
//                 "variables": [],
//                 "variableId": ""
//             },
//             "key": "others",
//             "type": "other",
//             "after": {
//                 "command": "toMatchIdCardNoAndPic"
//             }
//         }
//     ],
//     "errorRemind": {
//         "do": {
//             "data": "6",
//             "command": "speak",
//             "variables": [],
//             "variableId": ""
//         },
//         "after": {
//             "command": "jump-to",
//             "no": "BlankNode_53",
//             "id": "BlankNode_53"
//         },
//         "type": "no",
//         "maximum": 5
//     },
//     "completed": true,
//     "hasStartNode": false
// }
